package pseudoAssemblerCompiler.twopassassembler.model.io;

import java.io.*;

/**
 * User: Администратор
 * Date: 15.06.12
 * Time: 23:43
 */
public class ReaderImpl implements IReader {

    private void readFile(String fileName)
            throws FileNotFoundException, UnsupportedEncodingException {

    }

    @Override
    public void readFile(String fileName, ParserInterface parser)
            throws FileNotFoundException, UnsupportedEncodingException {
        InputStreamReader inputStreamReader;
        BufferedReader in;
        String s;
        ClassLoader classLoader = ReaderImpl.class.getClassLoader();
        InputStream is = classLoader.getResourceAsStream(fileName);
        inputStreamReader =
                new InputStreamReader(is, "UTF-8");
        in = new BufferedReader(inputStreamReader);

        try {
            try {
                while ((s = in.readLine()) != null) {
                    parser.parseString(s);
                }
            } finally {
                if(in != null) {
                    in.close();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void readFile(File file, ParserInterface parser)
            throws FileNotFoundException, UnsupportedEncodingException{
        InputStreamReader inputStreamReader;
        BufferedReader in;
        String s;
        inputStreamReader = new InputStreamReader(
                new FileInputStream(file.getAbsolutePath()), "UTF-8");

        in = new BufferedReader(inputStreamReader);

        try {
            try {
                while ((s = in.readLine()) != null) {
                    parser.parseString(s);
                }
            } finally {
                if(in != null) {
                    in.close();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
